
#include "create_win.h"

void CreateSubwin::init(WINDOW *main_win, int begin_x, int begin_y) {

    _win = subwin(main_win, _y, _x, begin_y, begin_x);
    box(_win, 0, 0);
    keypad(_win, true);
    refresh();
    wrefresh(_win);

}

void CreateWin::init(int begin_x, int begin_y) {

    _win = newwin(_y, _x, begin_y, begin_x);
    box(_win, 0, 0);
    keypad(_win, true);
    refresh();
    wrefresh(_win);

}

int CreateWin::ifButtonWsOrAdMin(vector_int &button, int highlight, int size) {

    if ((_choice == button[0]) || (_choice == button[1])) {
        if (highlight == -1) { return 0; }
        return --highlight;
    }

    if (highlight == -1)
        return 0;

    return highlight;

}


int CreateWin::ifButtonWsOrAdPlus(vector_int &button, int highlight, int size) {

    if ((_choice == button[0]) || (_choice == button[1])) {
        if (highlight == NUMBER(size)) { return NUMBER(size)-1; }
        return ++highlight;
    }

    if (highlight == NUMBER(size))
        return NUMBER(size)-1;

    return highlight;

}

void CreateWin::wsOrAdButton(const int size, const bool WS_or_AD) {
    getCh();

    pair<vector_int, vector_int> button_WS_or_AD;

    if (WS_or_AD) {
        button_WS_or_AD = {{KEY_W, UP}, {KEY_S, DOWN}};
    } else {
        button_WS_or_AD = {{KEY_A, LEFT}, {KEY_D, RIGHT}};
    }

    _highlight = ifButtonWsOrAdPlus(button_WS_or_AD.second, _highlight, size);
    _highlight = ifButtonWsOrAdMin(button_WS_or_AD.first, _highlight, size);

}

void CreateWin::wasdButton(const int size_x, const int size_y) {
    getCh();

    vector<pair<vector<int>, vector<int>>> button_WASD{
        {{KEY_W, UP}, {KEY_S, DOWN}}, 
        {{KEY_A, LEFT}, {KEY_D, RIGHT}}
    };

    _highlightY = ifButtonWsOrAdPlus(button_WASD[0].second, _highlightY, size_y);
    _highlightY = ifButtonWsOrAdMin(button_WASD[0].first, _highlightY, size_y);

    _highlightX = ifButtonWsOrAdPlus(button_WASD[1].second, _highlightX, size_x);
    _highlightX = ifButtonWsOrAdMin(button_WASD[1].first, _highlightX, size_x);

}

void CreateWin::clearWin(const int start_x, const int start_y,
                         const int finish_x, const int finish_y) {
    FOR_INT(i, finish_y) {
        FOR_INT(j, finish_x) {
            if (i > start_y && i < finish_y) {
                if (j > start_x && j < finish_x) {
                    mvwaddch(_win, i, j, ' ');
                }
            }
        }
    }

}

void CreateWin::drawWall(int wall_x, int wall_y, const bool drawWallXOrY) {
    string WALL;
    string WALL_90;
    string WALL_180;

    int numXOrY;

    if (drawWallXOrY) {
        WALL = WALL_Y_PERPENDICULAR;
        WALL_90 = WALL_Y_ANGLE_UNDER_90_DOWN;
        WALL_180 = WALL_Y_ANGLE_UNDER_90_TOP;
        
        numXOrY = _y;
    } else {
        WALL = WALL_X_IN_PARALLEL;
        WALL_90 = WALL_X_ANGLE_UNDER_90_LEFT;
        WALL_180 = WALL_X_ANGLE_UNDER_90_RIGHT;

        numXOrY = _x;

        int temp;

        temp = wall_x;
        wall_x = wall_y;
        wall_y = temp;
    }

    for (int i = numXOrY - wall_x; i < numXOrY; i++) {
        if (i != numXOrY - 1) {
            if (drawWallXOrY) {
                printStr(wall_x, i, WALL);
            } else {
                printStr(i, wall_y, WALL);
            }
        }
    }

    if (drawWallXOrY) {
        printStr(wall_x, _y - wall_y, WALL_90);
        printStr(wall_x, _y - 1, WALL_180);
    } else {
        printStr(_x - wall_x, wall_y, WALL_90);
        printStr(_x - 1, wall_y, WALL_180);
    }
    
}

void CreateWin::textSelection(const vector_str vec, const int x, const int y) {
    FOR_SIZE_T(i, vec.size()) {
        if (NUMBER(i) == _highlight) {
            w_attron(A_REVERSE);
        }

        printStr(x, y+i, vec[i]);
        w_attroff(A_REVERSE);
    }
}

void CreateWin::textSelection(const vector_str vec, function<void(int, vector_str)> func) {
    FOR_SIZE_T(i, vec.size()) {
        if (NUMBER(i) == _highlight) {
            w_attron(A_REVERSE);
        }

        func(i, vec);
        w_attroff(A_REVERSE);
    }
}

CreateWin::CreateWin(int x, int y, int begin_x, int begin_y, bool init)
    : _x(x), _y(y) {

    _highlight = 0;
    _highlightX = 0;
    _highlightY = 0;

    if (init) {
        this->init(begin_x, begin_y);
    }
}